﻿angular.module('app.masterdata.period', [])
    .config(['$routeProvider', function ($routeProvider) {
        $routeProvider

        .when('/admin/period', {
            templateUrl: 'app/masterdata/period/period-list.tpl.html',
            controller: 'periodConfigCtrl',
            resolve: {
                //首次加载第一页
                //                currentDeal: ['periodProxy', function (periodProxy) {
                //                    return periodProxy.searchInfo("Deal");
                //                } 
                //                ]

            }
        });
    } ])

//*****************************************header***************************s
    .controller('periodConfigCtrl', ['$scope', 'periodProxy', '$http', 'modalService',
    function ($scope, periodProxy, $http, modalService) {

        periodProxy.searchInfo("Deal", function (cdeal) {
            $scope.dealName = cdeal;
        });


        $scope.selectedLevel = 20;  //下拉单页容量初始化
        $scope.itemsperpage = 20;
        $scope.currentPage = 1; //当前页
        $scope.maxSize = 10; //分页显示的最大页
        var filstr = "";

        periodProxy.peroidPaging($scope.currentPage, $scope.itemsperpage, filstr, function (list) {
            $scope.totalItems = list[0].count;
            $scope.list = list[0].results;
        }, function (error) {
            alert(error);
        });

        //分页容量下拉列表定义
        $scope.levelList = [
                    { "id": 20, "levelName": '20' },
                    { "id": 500, "levelName": '500' },
                    { "id": 1000, "levelName": '1000' },
                    { "id": 2000, "levelName": '2000' },
                    { "id": 5000, "levelName": '5000' }
                    ];

        //加载nggrid数据绑定
        $scope.periodList = {
            data: 'list',
            columnDefs: [
                    { field: 'sortId', displayName: '#', width: '20' },
                    { field: 'periodBegin', displayName: 'Begin Date', width: '120',
                        cellTemplate: '<div class="ngCellText" ng-class="col.colIndex()">{{row.entity.periodBegin | date:"yyyy-MM-dd HH:mm:ss"}}</div>'
                    },
                    {
                        field: 'endDate', displayName: 'Payment Date', width: '120',
                        cellTemplate: '<div class="ngCellText" ng-class="col.colIndex()">{{row.entity.endDate | date:"yyyy-MM-dd HH:mm:ss"}}</div>'
                    },
                    { field: 'periodEnd', displayName: 'End Date', width: '120',
                        cellTemplate: '<div class="ngCellText" ng-class="col.colIndex()">{{row.entity.periodEnd | date:"yyyy-MM-dd HH:mm:ss"}}</div>'
                    },
                    { field: 'statusFlg', displayName: 'Status', width: '60' },
                    {
                        field: 'operatedate', displayName: 'Action Date', width: '120',
                        cellTemplate: '<div class="ngCellText" ng-class="col.colIndex()">{{row.entity.operatedate | date:"yyyy-MM-dd HH:mm:ss"}}</div>'
                    },
                    { field: 'operator', displayName: 'Collector', width: '100' },
                    {
                        name: 'SOA Check', displayName: 'SOA Task Validation', width: '380',
                        cellTemplate: '<div class="ngCellText" ng-class="col.colIndex()">' +
                                       '<a id = "lblagReport" ng-click="grid.appScope.detialSearch(row.entity.id);">AR Aging Report </a>&nbsp;' +
                                       '<img ng-show="row.entity.agingRepotFlg == 1" src="~/../Content/images/ready.png">' +
                                       '<img ng-show="row.entity.agingRepotFlg == 0" src="~/../Content/images/notready.png"> ' +
                                       '&nbsp;&nbsp;&nbsp;' +
                                       '<lable id = "lblagReport">One-Year Sales </lable>&nbsp;' +
                                       '<img ng-show="row.entity.oneYearsFlg == 1" src="~/../Content/images/ready.png">' +
                                       '<img ng-show="row.entity.oneYearsFlg == 0" src="~/../Content/images/notready.png"> &nbsp;&nbsp;&nbsp;' +
                                       '<a ng-show="row.entity.soaDoneFlg == 1" ng-click="grid.appScope.StartSOA()">{{row.entity.soaDone}} </a>' +
                                       '<label ng-show="row.entity.soaDoneFlg == 0">{{row.entity.soaDone}}</label></div>'
                    },
                    { field: 'description', displayName: 'Comments', width: '200' }
                    ]
        };
        //新账期开启
        $scope.addPeriod = function () {
            if ($scope.periodEndDate == null || $scope.periodEndDate =="") {
                alert("Please select new period's end date!");
                return;
            }
            else if($scope.paymentDate == null || $scope.paymentDate =="")
            {
                alert("Please select payment date!");
                return;
            }
            else {
                var icuPer = "0";
                periodProxy.searchInfo($scope.periodEndDate + "," + $scope.paymentDate, function (strres) {
                    icuPer = strres.toString();
                    if (icuPer == "2") {
                        alert("Please choose the future day!");
                        return;
                    }
                    if (icuPer == "1") {
                        var flg = confirm("Current will be Closed,It's OK?");
                        if (flg == false) {
                            return;
                        }
                    }
                    var perEndDate = [];
                    perEndDate.push($scope.periodEndDate);
                    perEndDate.push($scope.paymentDate);
                    periodProxy.addNewperoidByEndTime(perEndDate
                        , function (res) {
                            alert("Successed!");
                            periodProxy.peroidPaging($scope.currentPage, $scope.itemsperpage, filstr, function (list) {
                                $scope.totalItems = list[0].count;
                                $scope.list = list[0].results;
                            });
                        }
                , function (error) { alert(error) });
                });

            }
        };
        $scope.StartSOA = function () {
            //alert("SOA is doing now,Please wait!");
            periodProxy.startSOA("soa", function (res) {
                alert("Successed!");
                periodProxy.peroidPaging($scope.currentPage, $scope.itemsperpage, filstr, function (list) {
                    $scope.totalItems = list[0].count;
                    $scope.list = list[0].results;
                });
            }, function (error) {
                alert(error);
            })
        };

        $scope.detialSearch = function (id) {

            var modalDefaults = {
                templateUrl: 'app/masterdata/period/period-info.tpl.html',
                controller: 'periodInfoCtrl',
                resolve: {
                    periodDetail: function () {
                        return periodProxy.query({ id: id });
                    }
                }
            };
            modalService.showModal(modalDefaults, {}).then(function (result) {

            });
        };

        //单页容量变化
        $scope.pagesizechange = function (selectedLevelId) {
            var index = $scope.currentPage;
            periodProxy.peroidPaging($scope.currentPage, selectedLevelId, filstr, function (list) {
                $scope.itemsperpage = selectedLevelId;
                $scope.totalItems = list[0].count;
                $scope.list = list[0].results;
            }, function (error) {
                alert(error);
            });
        };

        //翻页
        $scope.pageChanged = function () {

            var index = $scope.currentPage;
            periodProxy.peroidPaging(index, $scope.itemsperpage, filstr, function (list) {
                $scope.list = list[0].results;
                $scope.totalItems = list[0].count;
            }, function (error) {
                alert(error);
            });
        }
    } ])

    .controller('periodInfoCtrl', ['$scope', '$modalInstance', 'periodDetail',
        function ($scope, $modalInstance, periodDetail) {

            $scope.list = periodDetail;
            $scope.agingReport = {
                data: 'list',
                columnDefs: [
                               { field: 'legalEntity', displayName: 'Legal Entity', width: '20%' }
                               ,
                                { name: 'account', displayName: 'Account Level Report', width: '40%',
                                    cellTemplate: '<div class="ngCellText" ng-class="col.colIndex()">' +
                                              '<img ng-show="row.entity.accTimes == 1" src="~/../Content/images/ready.png">' +
                                                   '<img ng-show="row.entity.accTimes == 0" src="~/../Content/images/notready.png"> </div>'
                                },
                                { name: 'invoice', displayName: 'Invoice Level Report', width: '40%',
                                    cellTemplate: '<div class="ngCellText" ng-class="col.colIndex()">' +
                                              '<img ng-show="row.entity.invTimes == 1" src="~/../Content/images/ready.png">' +
                                                   '<img ng-show="row.entity.invTimes == 0" src="~/../Content/images/notready.png"> </div>'
                                }
                                ]
            };


            $scope.close = function () {
                $modalInstance.close();
            };

        } ]);